package com.keli.dt.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.keli.dt.dao.IRoadReportDao;
import com.keli.dt.model.RoadReport;
import com.keli.dt.model.bpss.RoadReportDeleteBPSS;
import com.keli.dt.model.bpss.RoadReportInsertBPSS;

public class RoadReportDao extends JdbcDaoSupport implements IRoadReportDao {
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Override
	public int delete(String detectClass, String roadId) {
		String sql = RoadReportDeleteBPSS.DELETE_SQL;		
		logger.debug(sql.toString());		
		return this.getJdbcTemplate().update(sql, detectClass, roadId);
	}

	@Override
	public int insert(RoadReport roadReport, String reportTime) {	
		// 删除实时表
		delete(roadReport.getDETECT_CLASS(), roadReport.getROAD_ID().toString());
		// 插入实时表
		String realTimeSQL = RoadReportInsertBPSS.insertRealTimeSql();
		insert(realTimeSQL, roadReport, reportTime);
		// 插入历史表
		String historySQL = RoadReportInsertBPSS.insertHistorySql();
		insert(historySQL, roadReport, reportTime);
				
		return 1;
	}
	
	/**
	 * 插入路段新信息
	 * @param sql 插入语句
	 * @param rr 路段信息对象
	 * @param reportTime 上报时间
	 * @return
	 */
	private int insert(String sql, RoadReport rr, String reportTime) {
		logger.debug(sql);
		return this.getJdbcTemplate().update(sql, rr.getDETECT_CLASS(), 
				rr.getROAD_ID(), rr.getVOLUME_NONMOTOR(), rr.getFLOW_NONMOTOR(),
				rr.getVOLUME_MOTOR(), rr.getFLOW_MOTOR(), rr.getSPEED(), 
				rr.getCYCLE(), reportTime, rr.getDENSITY(), rr.getCOLLEC_TIME(), rr.getCOLLEC_DATETIME());
	}

	@Override
	public int insert(List<RoadReport> roadReports, String reportTime) {
		int i = 0;
		logger.debug("insert DT_ROAD_REPORT_H&R start\n");
		deleteWithBatchUpdate(roadReports);
		// 插入实时表
		String realTimeSQL = RoadReportInsertBPSS.insertRealTimeSql();
		insertWithBatchUpdate(realTimeSQL, roadReports, reportTime);
		// 插入历史表
		String historySQL = RoadReportInsertBPSS.insertHistorySql();
		insertWithBatchUpdate(historySQL, roadReports, reportTime);
		logger.debug("insert DT_ROAD_REPORT_H&R done\n");
		
		return roadReports.size();
	}
	
	private int insertWithBatchUpdate(String sql,
			List<RoadReport> roadReports, String reportTime) {
		logger.debug(sql);
		// 执行批量sql 处理多次插入操作
		int[] count = this.getJdbcTemplate().batchUpdate(sql,
				new RoadReportInsertBPSS(roadReports, reportTime));

		return 1;
	}
	
	private int deleteWithBatchUpdate(List<RoadReport> roadReports) {
		String sql = RoadReportDeleteBPSS.DELETE_SQL;
		logger.debug(sql);
		int[] count = this.getJdbcTemplate().batchUpdate(sql, new RoadReportDeleteBPSS(roadReports));
		
		return 1;
	}

	@Override
	public Integer findType(BigDecimal roadId) {
		String sql = "SELECT t.type FROM nt_zs.NT_FCDPROCESSED_T t WHERE t.renderid = ?";
		logger.debug(sql);
		try {
			return this.getJdbcTemplate().queryForObject(sql, Integer.class, roadId);
		} catch (Exception e) {
			logger.error("Not found type with roadId: " + roadId);
			return null;
		}
	}

}
